package core;

import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

import elements.Tuple;

public class ReduceWorker extends Thread {
	
	private String word;
	private ConcurrentLinkedQueue<Tuple> tuples;
	private ConcurrentLinkedQueue<Tuple> reduces;
	
	public ReduceWorker(String word, ConcurrentLinkedQueue<Tuple> tuples, 
								ConcurrentLinkedQueue<Tuple> reduces) {
		this.word = word;
		this.tuples = tuples;
		this.reduces = reduces;
	}

	public void run() {
		WordCounter.aliveWorkers++;
		int sum = 0;
		Iterator<Tuple> it = tuples.iterator();
		while (it.hasNext()) {
			Tuple tuple = it.next();
			sum += tuple.v2;
		}
		reduces.add(new Tuple(word, sum));
		WordCounter.aliveWorkers--;
	}
}
